import 'package:flutter/material.dart';
import 'package:flutterproject/mvvm/base/base_state.dart';
import 'package:flutterproject/page/mine/children/order/children/order_detail/order_detail_page.dart';
import 'package:flutterproject/page/mine/children/order/controller/order_list_controller.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/app_bar_common.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class OrderListPage extends BaseState<OrderListController> {
  @override
  OrderListController initController() => OrderListController();

  @override
  Widget initView(BuildContext context) {
    return DefaultTabController(
      length: 4,
      initialIndex: controller.currentIndex,
      child: ColoredBox(
        color: Color(0xFFF6F6F6),
        child: Column(
          children: [
            AppBarCommon(
              title: "我的订单",
              bgColor: Color(0xFFEDC88B),
              leftColor: Colors.white,
              titleColor: Colors.white,
            ),
            10.heightBox,
            TabBar(
              tabs: controller.myTabs,
              indicatorColor: Color(0xFFEDC88B),
              labelColor: Color(0xFFEDC88B),
              unselectedLabelColor: Color(0xFF414141),
              padding: EdgeInsets.zero,
              indicatorSize: TabBarIndicatorSize.label,
              indicatorPadding: MarginPaddingUtil.symmetric(vertical: 30.width),
              onTap: (index) => controller.changeIndex(index),
            ),
            Expanded(
              child: Padding(
                padding: MarginPaddingUtil.symmetric(horizontal: 20),
                child: ListView.separated(
                  padding: EdgeInsets.zero,
                  itemCount: controller.list.length,
                  separatorBuilder: (context, index) => 16.heightBox,
                  itemBuilder: (context, index) {
                    return _buildItem(Column(
                      children: [
                        Row(
                          children: [
                            TextCommon(
                              DateFormat("yyyy-MM-dd HH:mm:ss").format(
                                  DateTime.fromMillisecondsSinceEpoch(
                                      controller.list[index].createTime ?? 0)),
                              size: 20.sp,
                              lineHeight: 29.sp,
                            ),
                            Spacer(),
                            TextCommon(
                              controller
                                  .getStatusStr(controller.list[index].state),
                              size: 24.sp,
                              lineHeight: 35.sp,
                              color: controller.list[index].state == 3
                                  ? Color(0xFFA8A8A8)
                                  : Color(0xFFEDC88B),
                            ),
                          ],
                        ),
                        18.heightBox,
                        Row(
                          children: [
                            ImageCommon(
                              "/image/${controller.list[index].productTag}_2",
                              size: 128.width,
                              circular: 30.radius,
                            ),
                            32.widthBox,
                            Expanded(
                              child: Column(
                                children: [
                                  Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      TextCommon(
                                        controller.list[index].productName ??
                                            "",
                                        size: 28.sp,
                                        lineHeight: 40.sp,
                                      ),
                                      Spacer(),
                                      TextCommon(
                                        controller.list[index].totalAmount ?? "0.00",
                                        size: 28.sp,
                                        lineHeight: 40.sp,
                                        color: Color(0xFFE04D4D),
                                      ),
                                    ],
                                  ),
                                  12.heightBox,
                                  Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Expanded(
                                        child: TextCommon(
                                          controller.list[index]
                                                  .productDescription ??
                                              "",
                                          size: 24.sp,
                                          lineHeight: 32.sp,
                                          maxLines: 2,
                                        ),
                                      ),
                                      120.widthBox,
                                      TextCommon(
                                        "共${controller.list[index].productNum ?? "1"}件",
                                        size: 24.sp,
                                        lineHeight: 32.sp,
                                        color: Color(0xFFA8A8A8),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ],
                        )
                      ],
                    ),controller.list[index].id);
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildItem(Widget child,int? id) {
    return Container(
      padding:
          MarginPaddingUtil.fromLTRB(left: 40, top: 32, right: 36, bottom: 37),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.radius),
      ),
      child: child,
    ).addClickEvent(() => Get.to(OrderDetailPage(),arguments: id));
  }
}
